﻿package com.victim.snake.map
{
	import flash.events.EventDispatcher;
	import com.victim.snake.map.MapEvent;
	import flash.utils.Dictionary;
	import com.victim.snake.user.Main;
	import com.victim.snake.util.Loader;
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.display.LoaderInfo;
	import flash.utils.ByteArray;
	import com.victim.snake.user.ProcessMsg;

	public class Map extends EventDispatcher
	{
		static private var maps:Dictionary=new Dictionary();	//地图列表				
		private var name:String="";	//地图名(目录名)
		private var cName:String="";	//中文地图名
		private var backGroundImage:Bitmap=null;	//背景图片
		private var previewImage:Bitmap=null;	//缩略图		
		private var backGroundImageIsLoader:Boolean=false;	//背景图片是否加载完成
		private var previewImageIsLoader:Boolean=false;	//缩略图是否加载完成
		private var loadStatus:int=0;	//地图下载状态 0末加载 1下载中 2下载完成
		
		//构造函数
		public function Map(name:String)
		{
			this.name=name;
		}
		
		public function load():void{
			//如果map末加载过
			if(!this.isLoader()&&loadStatus==0){
				loadStatus=1;
				var loader:Loader=new Loader("image/map/"+name+"/backGround.jpg",this.landImageLoaderEvent);	//下载背景图
				var previewLoader:Loader=new Loader("image/map/"+name+"/preview.jpg",this.previewLoaderEvent);	//下载缩略图
			}else{
				this.isLoaderEvent();
			}
		}
		
		//取得地图
		static public function getMap(mapName:String):Map{			
			if(Map.maps[mapName]==undefined){
				Map.maps[mapName]=new Map(mapName);
			}
			return Map.maps[mapName];
		}
		
		//向服务器发送加载所有地图请求
		static public function getAllMap():void{
			var ba:ByteArray=new ByteArray();
			ba.writeByte(22);			//消息类型;
			ba.writeShort(0);			//消息内容的长度;
			ProcessMsg.socketSend(ba);
			ba.clear();
		}
		
		//背景图片
		function landImageLoaderEvent(event:Event):void{
			//trace("下载完成2");
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.backGroundImage=Bitmap(loaderInfo.content);
			this.backGroundImageIsLoader=true;
			this.isLoaderEvent();
			//trace("背景下载完成");
		}		
		
		//缩略图
		function previewLoaderEvent(event:Event):void{
			var loaderInfo:LoaderInfo=LoaderInfo(event.target);
			this.previewImage=Bitmap(loaderInfo.content);
			this.previewImageIsLoader=true;
			this.isLoaderEvent();
			//trace("缩略图下载完成");
		}
		
		public function isLoader():Boolean{
			//trace(this.backGroundImageIsLoader && this.previewImageIsLoader);
			return this.backGroundImageIsLoader && this.previewImageIsLoader;
		}
		
		//如果下载完成,抛出事件
		public function isLoaderEvent():void{
			if(this.isLoader()){
				loadStatus=2;
				var mapEvent:MapEvent=new MapEvent(MapEvent.IsLoader,this);
				this.dispatchEvent(mapEvent);
			}
		}
		
		//get and set
		
		public function getName():String{
			return this.name;
		}
		
		public function setName(name:String):void{
			this.name=name;
		}
		
		public function getCName():String{
			return this.cName;
		}
		
		public function setCName(cName:String):void{
			this.cName=cName;
		}
		
		public function getBackGroundImage():Bitmap{
			return this.backGroundImage;
		}
		
		public function getPreviewImage():Bitmap{
			return this.previewImage;
		}
		
		static public function getMaps():Dictionary{
			return Map.maps;
		}
	}

}